<?php
error_reporting(E_ALL);
require_once dirname(__FILE__) . '/../../php/texyla.php';

// přiřazení ikonek k typům souborů podle jejich extense
// Nutno připravit obrázky a jejich názvy upravit podle potřeby


class texylaFileBrowser {
	
	public $ROOT = '/';
	public $ikonsForFileType = array (
		// obrázky *
		'img' => array (
			'jpg',
			'gif',
			'png',
			'jpeg',
		),
		// Flash *
		'flash' => array (
			'swf',
		),
		// komprimované *
		'package' => array (
			'gz',
			'zip',
			'tgz',
			'tar',
			'rar',
			'arj',
			'deb',
		),
		// documents *
		'doc' => array (
			'pdf',
			'rtf',
			'doc',
		),
		// audio 
		'sound' => array (
			'mp3',
			'ogg',
		),
		// video *
		'video' => array (
			'mp2',
			'ogm',
			'avi',
			'mpga',
			'mpeg'
		),
	);
	public function __construct() {	
	}
	
	public function getDir($dirName = '', $level = 0) {
		if(empty($dirName)){
			$dirName = $this->ROOT;// nastavit podle své potřeby přes $this->ROOT
		}
		$a = null;
		$dirName = realpath($dirName);
		$dir =  scandir($dirName);
		foreach($dir as $file) {
			$filePath = "$dirName/$file";
			$typ = filetype($filePath);
			$filesize = filesize($filePath);
			$extension = $this->getFileExtension($filePath);
			$imagesize = $this->getSizeImage($filePath);
			$iconType = $this->getFiletypeIcon($extension);
			if ($typ == 'file') {
			/**
			"name" => stripExtension($file),
			"href" => TEXYLA_FILE_DIR . $file,
			"title" => "Download file $file",
			"icon" => $icon,
			"size" => ceil(filesize($filePath) / 1024) . "kB",
			*/
				$a[] = array(
					'name' => $file,
					'href' => TEXYLA_FILE_DIR . $file,
					'title' => "Directory $file",
					'icon' => $iconType,
					'size' => $filesize,
					'filetype' => $typ,
					'extension' => $extension,
					'imagesize' => $imagesize,
					
				);
			} else if ($typ == 'dir') {
				if ($file == '.' or $file == '..') {
				} else {
					$a[] = array(
					'name' => $file,
					'href' => TEXYLA_FILE_DIR . $file,
					'title' => "Download file $file",
					'icon' => 'dir',
					'size' => $filesize,
					'filetype' => $typ,
					'extension' => $extension,
					'subdir' => $this->getDir($filePath, ($level + 1)),
					);
				}
			} else {
				$a[] = array(
					'name' => $file,
					'href' => TEXYLA_FILE_DIR . $file,
					'title' => "Download file $file",
					'icon' => $iconType,
					'size' => $filesize,
					'filetype' => $typ,
					'extension' => $extension,
					'imagesize' => $imagesize,
				);
			}
		}
		return $a;	
	}

	private function getFileExtension($name) {
		$a = explode('.', $name);
		return strtolower($a[count($a) - 1]);  
	}

	private function getSizeImage($file){
		$size = @getImageSize($file);
		if (is_array($size)) {
			return array($size[0], $size[1]);
		}
		return null;
	}
	private function getFiletypeIcon($extension) {
		// získáme jméno ikonky 
		foreach ($this->ikonsForFileType as $key => $array) {
			if (in_array($extension, $array)) {
				return $key;
			}
		}
		return null;
	}

}

$v = new texylaFileBrowser;

$v->ROOT = dirname(__FILE__) . '/../../../files/';
//echo print_r($v->getDir()); 
echo json_encode(array('files' => $v->getDir()));

?>
